﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Transactions;
using BSU.GraduateWork.Data.Model;
using BSU.GraduateWork.Data.Repositories.Interfaces;

namespace BSU.GraduateWork.Data.Repositories
{
    public class ProblemRepository : BaseRepository<Problem3D, int>, IProblemRepository
    {
        #region IProblemRepository Members

        

        #endregion

        public override Problem3D Get(int id)
        {
            var problem = base.Get(id);

            var pointRepository = new PointRepository();
            problem.Solution.U = pointRepository.List(problem.SolutionId);

            return problem;
        }

        public override Problem3D Save(Problem3D entity)
        {
            using (var ts = new TransactionScope())
            {
                var context = GetNewContext();
                entity = base.Save(entity, context);

                var solutionPoints = entity.Solution.U.Select(point => new SolutionPoint3D
                                                                           {
                                                                               X = point.X, Y = point.Y, Z = point.Z, U = point.U, IsBoundary = point.IsBoundary, SolutionId = entity.SolutionId
                                                                           }).ToList();

                var pointRepository = new PointRepository();
                pointRepository.SaveAll(solutionPoints);

                ts.Complete();
            }
            return entity;
        }
    }
}